export default {
    methods: {
      async loadDataFromDB() {
        const required = window.indexedDB.open('localDB', 1);
        required.onupgradeneeded = (event) => {
          const db = event.target.result;
          if (!db, ObjectStoreNames.contains('items')) {
            db.createObjectStore('items', { keyPath: 'id', autoIncrement: true });
          }
        };
        request.onsuccess = (event) => {
          const db = event.target.result;
          const transaction = db.transaction(['items'], 'readonly');
          const objectStore = transaction.objectStore('items');
          const getRequest = objectStore.getAll();
          getRequest.onsuccess = (event) => {
            this.items = event.target.result;
          };
          getRequest.onerror = (error) => {
            console.error('加载数据失败', error);
          };
        };
      },
      async addItem() {
        const request = window.indexedDB.open('localDB', 1);
        request.onsuccess = (event) => {
          const db = event.target.result;
          const transaction = db.transaction(['items'], 'readwrite');
          const objectStore = transaction.objectStore('items');
          const addRequest = objectStore.add({ content: this.newItem });
          addRequest.onsuccess = () => {
            this.newItem = '';
            this.loadDataFromDB();
          };
          addRequest.onerror = (error) => {
            console.error('添加数据失败', error);
          };
        };
      }
    }
  };